<?php
namespace Admin\Controller;
use Think\Controller;
class CouponentityController extends CommonController{
	//查优惠券列表
    public function index(){
       // import('ORG.Util.Page');
    	$count = M('couponentity_list')->count();
        $Page  = new \Think\Page($count,15);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $this->page  = $Page->show();
    	$res   = M('couponentity_list')->limit($Page->firstRow.','.$Page->listRows)->select();
    	foreach ($res as $v){
    	    $v['use_time']=($v["use_time"]/3600/24);
    	    $v['lost_cycle']=($v["lost_cycle"]/3600/24);
    	    $rel[]=$v;
        }
        $this->assign('list',$rel);
        $this->display();
    }

	//添加优惠券
	public function add(){
		if(IS_POST) {
                    $data = I('post.');
                    $data['start_time']=strtotime($data['start_time']);
                    $data['end_time']=strtotime($data['end_time']);
                    $data['use_time']=(intval($data['use_time']))*3600*24;
                    $data['lost_cycle']=(intval($data['lost_cycle']))*3600*24;
                    $rel =  M('couponentity_list')->add($data);

                if($rel){
                        $this->success("添加成功！",U('index'),1);
                        die;
                    }else{
                        $this->error("添加失败！",U(''),1);
                }
        }
        $this->display();
    }
	//删除
	public function dels(){
        $role=I("get.id");
        $data['state']=0;
        $result=M("couponentity_list")->where("entity_coupon_id='".$role."'")->save($data);
		//dump($result);die;
        if($result){
            $this->success("删除成功！","",1);
        }else{
            $this->error("删除失败！","",1);
        }
    }
	 //编辑
	 function edit(){
             $role=I("get.id");
             $res=M("couponentity_list")->where(array("entity_coupon_id"=>$role))->find();
             $res['use_time']=($res["use_time"]/3600/24);
             $res['start_time']=date("Y-m-d",$res['start_time'])."T".date("H:i:s",$res['start_time']);
             $res['end_time']=date("Y-m-d",$res['end_time'])."T".date("H:i:s",$res['end_time']);
             $res['lost_time']=date("Y-m-d",$res['lost_time'])."T".date("H:i:s",$res['lost_time']);
             $res['lost_cycle']=($res["lost_cycle"]/3600/24);
             $this->res=$res;



        if(IS_POST){
            $data=I("post.");

            if ($data) {
                $data['start_time'] = strtotime($data['start_time']);
                $data['end_time'] = strtotime($data['end_time']);
                $data['lost_time'] = strtotime($data['lost_time']);
                $data['use_time'] = (intval($data['use_time'])) * 3600 * 24;
                $data['lost_cycle'] = (intval($data['lost_cycle'])) * 3600 * 24;
                $res = M("couponentity_list")->save($data);
                if ($res) {
                    $this->success("修改成功！", U('Couponentity/index'), 1);
                    die;
                } else {
                    $this->error("修改失败！", "", 1);
                }
            }

        }

             $this->display();
    }
            
        
}
